//
//  WBMainTarBarController.swift
//  wiboDemo
//
//  Created by shizb macbook air on 17/7/29.
//  Copyright © 2017年 YL. All rights reserved.
//

import UIKit

class WBMainTarBarController: UITabBarController {
     lazy var composeButton:UIButton=UIButton()

    override func viewDidLoad() {
        super.viewDidLoad()
        setUpChildControllers()
        setUpComposeButton()
    }
    func composeBtn()  {
        print("l,llll")
    }
    //用代码支持横屏
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask{
        return .portrait
    }
    
}


extension WBMainTarBarController {
    //设置中间按钮
    func setUpComposeButton() {
        tabBar.addSubview(composeButton)
        
        let count=CGFloat(childViewControllers.count)
        let w=tabBar.bounds.width / count-1
        composeButton.frame=tabBar.bounds.insetBy(dx: 2 * w, dy: 0)
        composeButton.setImage(UIImage(named:"tabbar_compose_icon_add"), for: .normal)
        composeButton.setBackgroundImage(UIImage(named:"tabbar_compose_button"), for: .normal)
        composeButton.setImage(UIImage(named:"tabbar_compose_icon_add_highlighted"), for: .selected)
        composeButton.setBackgroundImage(UIImage(named:"tabbar_compose_button_highlighted"), for: .selected)
        
        composeButton.addTarget(self, action: #selector(composeBtn), for: .touchUpInside)
    }
    //设置五个按钮
    func setUpChildControllers() {
        let array:[[String:Any]]=[
                    ["clsName":"HomeViewController","title":"首页","imageName":"home","visitorInfo":["imageName":"ss","message":"关注一些人"]],
                    ["clsName":"MessageViewController","title":"消息","imageName":"message_center","visitorInfo":["imageName":"","message":"等芦潮港看健康快乐健康"]],
                    ["clsName":"UIViewController"],
                    ["clsName":"DiscoverViewController","title":"发现","imageName":"discover","visitorInfo":["imageName":"","message":"不会小奇偶及"]],
                    ["clsName":"ProfileViewController","title":"我","imageName":"profile","visitorInfo":["imageName":"","message":"贾斯丁就是六角航道局客流量"]]]
        var arrayM=[UIViewController]()
        for dict in array {
            arrayM.append(controller(dict: dict))//调用controller方法
        }
        viewControllers=arrayM
    }
    
      //获取字典内容
    func controller(dict:[String:Any]) -> UIViewController {
        //获取字典内容
        guard let title=dict["title"] as? String ,
        let imageName=dict["imageName"] as? String,
        let clsName=dict["clsName"] as? String,
        let cls=NSClassFromString(Bundle.main.namespace + "." + clsName) as? UIViewController.Type
        else {
            return UIViewController()
        }
        
        let vc=cls.init()
        vc.title = title
    
    
        //设置图片
        vc.tabBarItem.image=UIImage(named: "tabbar_" + imageName)
        vc.tabBarItem.selectedImage=UIImage(named: "tabbar_" + imageName + "selected")?.withRenderingMode(.alwaysOriginal)
        //改变标签栏字体颜色、大小
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orange], for: .highlighted)
        vc.tabBarItem.setTitleTextAttributes([NSFontAttributeName:UIFont.systemFont(ofSize: 14)], for: UIControlState(rawValue:0))
        
        let nav=WBNavigationController(rootViewController: vc)
        return nav
    }
    
}






























